<?php

include_once 'include.php';
$template = new template("Donations");
$donations = new donations();
$menu = menu();
$use = $db->get_row("Select * from Users where id='".$_SESSION['id']."'");
$page = "list";
$id = 0;
$message = null;
if(isset ($_GET["page"]))$page = $_GET["page"];
if(isset ($_GET["id"]))$id = $_GET["id"];
//if(isset ($_GET["message"]))$message = $_GET["message"];
//if(isLoggedIn())
//{
    switch ($page)
    {
        case "showDonations":
            $template->addContent($donations->showDonations($id));
            break;
        case "addDonation":
            $template->addContent($donations->addDonation($id));
        break;

        case "delete":
            $donations->deleteDonation($id);
        break;

        case "added":
            $donations->added();
        break;
        default:
            break;
    }
//}
//else
//{
//$template->addContent("You are not authorized to view this page. Please <a href = 'login.html'> Log In </a>");
//}
$template->showMenu($menu);
$template->showLogin("Welcome");
$template->showUser($use->name.' | <a href="logout.php" class="logout">Logout</a>');
$template->genPage();
class donations
{
        function addDonation($id)
        {
            $page = "<h2> Add Donation </h2><div class='form'>
         <form action='donations.php?page=added' method='post' class='niceform'>
         
                <fieldset>
                <input type ='hidden' value = '".$id."'name='id'/>
                    <dl>
                        <dt><label for='username'>Details:</label></dt>
                        <dd><textarea  name='details' id=''  rows='10' cols = '40'></textarea></dd>
                    </dl>
                    
                    <dl>
                        <dt><label for='amount'>Amount:</label></dt>
                        <dd><input type = 'text'  name='amount' id='' value='0' class='date-pick' /></dd>
                    </dl>
                    <dl class='submit'>
 <input type='submit' name='submit' id='submit' value='Save' /> </dl> </form>";
            return $page;
        }

        function showDonations($id)
        {
            Global $db;
            $donate = $db->get_results("SELECT * FROM donations WHERE donorId=".$id);
            $data = "<table><tr><td>";
            foreach($donate as $donation)
            {
                $data .= $donation->timestamp."</td><td>";
                $data .= $donation->amount."</td><td>";
                $data .= $donation->details."</td></tr>";
            }
            return $data."</table>";
        }
        
        function added()
        {
            Global $db;
            $id = $_POST['id'];
            $details = $_POST['details'];
            $amount = $_POST['amount'];
            $query = "INSERT INTO donations (donorId, timestamp, amount,details) VALUES (".$id.", Now(),".$amount.",'".$details."')";
            $db->query($query);
            
            header("Location:donor.php?page=item&id=".$id."&message=Donation Added");
        }
        
        function deleteDonation($id)
        {
            Global $db;
            $did = $db->get_var("Select donorId FROM donations WHERE id=".$id);
            $this->deleteDonations($id);
            //$db->query("Delete FROM donations WHERE id=".$id);
             header("Location:donor.php?page=item&id=".$did."&message=Donation Deleted");
        }
        
        
    function deleteReminders($id, $type)
    {
        Global $db;
        $db->query("DELETE FROM reminder WHERE typeId = ".$id." AND type=$type");
    }
    
 function deleteDonations($id)
    {
        Global $db;
       // $com = $db->get_results("Select * from donations WHERE id = ".$id);
       // if($com)
       // foreach ($com as $theCom)
       // {
            $this->deleteReminders($id,2);
       // }
        $db->query("DELETE FROM donations WHERE id = ".$id);
    }
    
    
}
?>